#include "ConferenceModule.h"

ConferenceModule::ConferenceModule(int mode, int basePort, int port, uint8_t ip[4]) // 1 = send, 2= receive
{
	m_mode = mode;
	m_basePort = basePort;
	m_port = port;

	m_ip[0] = ip[0];
	m_ip[1] = ip[1];
	m_ip[2] = ip[2];
	m_ip[3] = ip[3];



}

ConferenceModule::~ConferenceModule(void)
{
	audioSess->destroy();

	
}

void ConferenceModule::run()
{
	switch(m_mode)
	{
	case 1: startSending();
		break;
	case 2: startReceiving();
		break;
	default:
		break;
	}
}

void ConferenceModule::checkRet(bool ret,const MIPErrorBase &obj)
{
	if (!ret)
	{
		std::cerr << obj.getErrorString() << std::endl;
		exit(-1);
	}
}


void ConferenceModule::setCodec(int codecNr)
{
	usedCodec = codecNr;
}

// sender sound
int ConferenceModule::startSending()
{
	std::cout << "Start sending..." << std::endl;
	std::cout << "Start sending to port " << m_port << std::endl;
	MIPAudioSessionParams Aparams;
	audioSess = new MyAudioSession;
	bool ret;

	int bw = 20000;

	


	Aparams.setPortbase(m_basePort);
	switch(usedCodec)
	{
	case 0:
		Aparams.setCompressionType(MIPAudioSessionParams::ULaw);
		break;
	case 3:
		Aparams.setCompressionType(MIPAudioSessionParams::GSM);
		break;
	case 7:
		Aparams.setCompressionType(MIPAudioSessionParams::LPC);
		break;
	case 8:
		Aparams.setCompressionType(MIPAudioSessionParams::ALaw);
		break;
	case 11:
		Aparams.setCompressionType(MIPAudioSessionParams::L16Mono);
		break;
	case 98:
		Aparams.setCompressionType(MIPAudioSessionParams::Speex);
		Aparams.setSpeexEncoding(MIPAudioSessionParams::NarrowBand);
		break;
	case 99:
		Aparams.setCompressionType(MIPAudioSessionParams::Speex);
		Aparams.setSpeexEncoding(MIPAudioSessionParams::WideBand);
		break;
		
	}

	Aparams.setAcceptOwnPackets(false);
	ret = audioSess->init(&Aparams);
	checkRet(ret, *audioSess);

	cout << "Audio init: " << ret << std::endl;

	ret = audioSess->addDestination(RTPIPv4Address(m_ip,m_port));

	cout << "Destination init: " << m_ip << m_port << std::endl;
	
	while(1)
		sleep(1000);

	return 0;
}

// receiver sound
int ConferenceModule::startReceiving()
{
	std::cout << "Start receiving..." << std::endl;
	MIPAudioSessionParams Aparams;
	audioSess = new MyAudioSession;
	bool ret;

	int bw = 20000;

	switch(usedCodec)
	{
	case 0:
		Aparams.setCompressionType(MIPAudioSessionParams::ULaw);
		break;
	case 3:
		Aparams.setCompressionType(MIPAudioSessionParams::GSM);
		break;
	case 7:
		Aparams.setCompressionType(MIPAudioSessionParams::LPC);
		break;
	case 8:
		Aparams.setCompressionType(MIPAudioSessionParams::ALaw);
		break;
	case 11:
		Aparams.setCompressionType(MIPAudioSessionParams::L16Mono);
		break;
	case 98:
		Aparams.setCompressionType(MIPAudioSessionParams::Speex);
		Aparams.setSpeexEncoding(MIPAudioSessionParams::NarrowBand);
		break;
	case 99:
		Aparams.setCompressionType(MIPAudioSessionParams::Speex);
		Aparams.setSpeexEncoding(MIPAudioSessionParams::WideBand);
		break;
		
	}

	ret = audioSess->init(&Aparams);
	checkRet(ret, *audioSess);

	//uint8_t ipLocal[4] = { 127,0,0,1 };
	audioSess->addToAcceptList(RTPIPv4Address(m_ip,m_port));
	cout << "Receiver ontvangt van poort " << m_port;

	while(1)
		sleep(1000);

	return 0;
}
